package com.shujiaotong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shujiaotong.common.R;
import com.shujiaotong.mapper.UserCardMapper;
import com.shujiaotong.mapper.UserClassMapper;
import com.shujiaotong.pojo.*;
import com.shujiaotong.mapper.UserMapper;
import com.shujiaotong.service.ICardService;
import com.shujiaotong.service.IClassesService;
import com.shujiaotong.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserCardMapper userCardMapper;

    @Autowired
    private ICardService cardService;

    @Autowired
    private UserClassMapper userClassMapper;

    @Autowired
    private IClassesService classesService;

    @Override
    public List<UserCard> getUserCardsByUserid(Long userid) {
        //根据用户id 获取卡片信息
        LambdaQueryWrapper<UserCard> qw = new LambdaQueryWrapper<>();
        qw.eq(UserCard::getUserId,userid);
        List<UserCard> userCards = userCardMapper.selectList(qw);
        return userCards;
    }


    @Transactional
    @Override
    public boolean saveByCardIdToUser(Long cardId, User user) {
        // 往userCard表新增数据save
        UserCard userCard = new UserCard();
        userCard.setUserId(user.getId());
        userCard.setCardId(cardId);

        // 查询card表
        Card card = cardService.getById(cardId);
        // cardName-name
        userCard.setCardName(card.getName());
        // starttime立即生效,endtime 根据卡的天数计算没有
        userCard.setStartDate(LocalDate.now());
        userCard.setEndDate(
                userCard.getStartDate()
                        .plusDays(card.getDay()) // 往后推 n 天...
        );
        // tags\type\count\isD\img\pr 名字和另一个对象参数名相同
        // 对象属性copy  A--->B  忽略属性
        //id一定不能copy，status\4大件也不能，创建时间、更新时间、创建人、更新人
        BeanUtils.copyProperties(card,userCard,
                "id","status","updateTime","createTime",
                "updateUser","createUser"
        );
        //status 手动赋值
        userCard.setStatus(1);

        int i = userCardMapper.insert(userCard);

        return i>0?true:false;
    }



    @Override
    public List<Classes> userClassesByUserId(Long userId) {
        // 1.根据用户ID 查询 课程ID
        LambdaQueryWrapper<UserClass> qw = new LambdaQueryWrapper<>();
        qw.select(UserClass::getClassId);// 查询投影
        qw.eq(UserClass::getUserId,userId);
        List<UserClass> userClasses = userClassMapper.selectList(qw);
        // 2. 把 UserClass ----> Long
        ArrayList<Long> ids = new ArrayList<>();
        for (UserClass userClass : userClasses) {
            ids.add(userClass.getClassId());
        }

        // 3. 根据ID集合查询课程
        List<Classes> classes = classesService.listByIds(ids);

        return classes;
    }

    @Override
    public R getUserByClassId(Long id) {
//        传入老师的id，通过classes表的teacherid列查询对应的所有class信息
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.eq(Classes::getTeacherId,id);
        return R.success(classesService.list(qw));

    }

    @Override
    public boolean getSelectedCard(Long userId, Long cardId) {

        LambdaQueryWrapper<UserCard> qw = new LambdaQueryWrapper<>();
        qw.eq(UserCard::getUserId, userId);
        qw.eq(UserCard::getCardId, cardId);
        qw.lt(UserCard::getEndDate, LocalDateTime.now());

        UserCard userCard = userCardMapper.selectOne(qw);

        if (userCard == null) {
            return false;
        }
        return true;
    }

}
